//
//  ViewController.m
//  HomeWork_自定义Cell
//
//  Created by 黄威 on 16/6/21.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "AddressBook.h"
#import "ContactTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)AddressBook *addressbook;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handData];
    [self createTableView];
}

- (void)config{

    self.navigationItem.rightBarButtonItem = [self editButtonItem];
    
    self.navigationItem.title = @"UT通讯录";
    
    self.addressbook = [[AddressBook alloc]init];
    
    self.automaticallyAdjustsScrollViewInsets = NO;


}

- (void)handData{

    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@".plist"];
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    
    NSArray *ArrayOfKey = [dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString *key  in ArrayOfKey) {
        
        NSArray *contactArray = [dictionary objectForKey:key];
 
        NSMutableArray *arrayOfContact = [NSMutableArray arrayWithCapacity:10];
        
        for (NSDictionary *dic in contactArray) {
            
            Contact *contact = [[Contact alloc]init];
            
            [contact setValuesForKeysWithDictionary:dic];
            
            [arrayOfContact addObject:contact];
            
        }
    
        [self.addressbook.DicOfContact setObject:arrayOfContact forKey:key];
        
        
      //  NSLog(@"%@",self.addressbook.DicOfContact);

    }
    
    
   

}




- (void)createTableView{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain];

    [self.view addSubview:self.tableView];


    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    
    [self.tableView registerClass:[ContactTableViewCell class] forCellReuseIdentifier:@"pool"];




}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   
    return  self.addressbook.DicOfContact.count;;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSArray *groupNames = [self.addressbook getGroupNames];
    
    NSString *key = [groupNames objectAtIndex:section];
    
    NSArray *array = [self.addressbook.DicOfContact objectForKey:key];
    
    
    
    NSLog(@"%ld",array.count);
    
    return array.count;


}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    
    ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    NSArray *groupName = [self.addressbook getGroupNames];
    NSArray *contactArray = [self.addressbook.DicOfContact objectForKey:[groupName objectAtIndex:indexPath.section]];
    
    Contact *contact = [contactArray objectAtIndex:indexPath.row];
    
    cell.model = contact;
    
    
    
    
    NSLog(@"%@",cell);
    
    return cell;



}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 120;


}








































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
